﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LuDo.PitEditor
{
    class BBTag
    {
        public enum TagType { None, Unknown, Bold, Underline, Italic, Strikethrough, SuperScript, Code, List, ListItem, Link };
        public enum TagState { Opening, Closing }
        public TagType Type { get; private set; }
        public TagState State { get; private set; }
        public string TagText { get; private set; }
        //List type properties
        public enum ListTagType { None, Unsorted, Sorted, Alphabetic };
        public ListTagType ListType { get; private set; }
        //Link type properties
        public string URL { get; private set; }
        public string TargetLink { get; private set; }
        public BBTag()
        {
            this.Type = TagType.None;
        }

        static public BBTag GetTag(string Text)
        {
            char carattereCorrente = Text[0];
            bool IsTag = carattereCorrente == '[';
            if (!IsTag)
            {
                return null;
            }
            else
            {
                int ClosingPos = Text.IndexOf(']');
                if (ClosingPos == -1)
                {
                    throw new ApplicationException("Testo errato.");
                }
                else
                {
                    BBTag tag = new BBTag();
                    tag.TagText = Text.Substring(0, ClosingPos + 1);
                    tag.State = tag.TagText[1] != '/' ? TagState.Opening : TagState.Closing;

                    //Determinazione del tipo di tag
                    //[b][/b]
                    //[u][(u]
                    //[i][/i]
                    //[s][/s]
                    //[sup][/sup]
                    //[code][size=10][/size][/code]
                    //[list][/list] [list=1][/list] [list=a][/list] [*]
                    //[url=xxx t=yyy][/url]
                    string[] SplitTag = tag.TagText.Split(new char[] { '[', ']', '/', '=', ' ' }).Where(c => c != "").ToArray(); ;
                    string id = SplitTag[0];
                    tag.Type = id == "b" ? TagType.Bold : id == "u" ? TagType.Underline :
                        id == "i" ? TagType.Italic : id == "s" ? TagType.Strikethrough :
                        id == "sup" ? TagType.SuperScript :
                        id == "code" ? TagType.Code : id == "list" ? TagType.List : 
                        id == "*" ? TagType.ListItem : id == "url" ? TagType.Link : TagType.Unknown;
                    if (tag.Type == TagType.Unknown)
                        return null;
                    if (tag.State == TagState.Opening)
                    {
                        if (tag.Type == TagType.List)
                        {
                            tag.ListType = SplitTag.Count() == 1 ? ListTagType.Unsorted :
                                SplitTag[1] == "1" ? ListTagType.Sorted :
                                SplitTag[1] == "a" ? ListTagType.Alphabetic : ListTagType.Unsorted;
                        }
                        else if (tag.Type == TagType.Link)
                        {
                            int startpos = tag.TagText.IndexOf('=') + 1;
                            int endpos = tag.TagText.IndexOf(' ', startpos);
                            tag.URL = tag.TagText.Substring(startpos, endpos - startpos);
                            tag.TargetLink = SplitTag.Last();
                        }
                    }

                    return tag;
                }
            }
        }
    }
}
